using System;
using MintAnimation.Core;
using MintAnimation.Runtime.Core.MintAnim;
using UnityEngine;

namespace Utils
{
    public static class TransformExtend
    {

        public static void SetVector2Int(this Transform transform, Vector2Int vector2Int, bool isLocal = true)
        {
            if (isLocal)
            {
                transform.localPosition = new Vector3(vector2Int.x, 0, vector2Int.y);
            }
            else
            {
                transform.position = new Vector3(vector2Int.x, 0, vector2Int.y);
            }
        }

        public static void SetPointInt(this Transform transform, PointInt vector2Int, bool isLocal = true)
        {
            if (isLocal)
            {
                transform.localPosition = new Vector3(vector2Int.x, 0, vector2Int.y);
            }
            else
            {
                transform.position = new Vector3(vector2Int.x, 0, vector2Int.y);
            }
        }

        public static Vector2Int GetVector2Int(this Transform transform, bool isLocal = true)
        {
            if (isLocal)
            {
                return new Vector2Int((int) (transform.localPosition.x + 0.5f), (int) (transform.localPosition.z + 0.5f));
            }
            else
            {
                return new Vector2Int((int) (transform.position.x + 0.5f), (int) (transform.position.z + 0.5f));
            }
        }

        public static void PosX(this Transform transform, float x, bool isLocal = true)
        {
            var temp = isLocal ? transform.localPosition : transform.position;
            if (isLocal)
            {
                transform.localPosition = new Vector3(x, temp.y, temp.z);
            }
            else
            {
                transform.position = new Vector3(x, temp.y, temp.z);
            }
        }

        public static void PosY(this Transform transform, float y, bool isLocal = true)
        {
            var temp = isLocal ? transform.localPosition : transform.position;
            if (isLocal)
            {
                transform.localPosition = new Vector3(temp.x, y, temp.z);
            }
            else
            {
                transform.position = new Vector3(temp.x, y, temp.z);
            }
        }

        public static void PosZ(this Transform transform, float z, bool isLocal = true)
        {
            var temp = isLocal ? transform.localPosition : transform.position;
            if (isLocal)
            {
                transform.localPosition = new Vector3(temp.x, temp.y, z);
            }
            else
            {
                transform.position = new Vector3(temp.x, temp.y, z);
            }
        }

        public static MintTween MoveTo(this Transform transform, Vector3 startPos, Vector3 endPos, float delay, MintEaseMethod easeMethod = MintEaseMethod.Linear, bool isLocal = false, Action callback = null)
        {
            MintPlayerPosition playerPosition = new MintPlayerPosition();
            playerPosition.StartValue = startPos;
            playerPosition.EndValue   = endPos;
            playerPosition.TweenOptions = new MintTweenOptions()
                                          {
                                              Duration = delay,
                                              EaseType = easeMethod
                                          };
            playerPosition.IsLocal          = isLocal;
            playerPosition.IsAutoStartValue = false;
            playerPosition.Init(transform);
            playerPosition.Tween.OnComplete += callback;
            playerPosition.Tween.Start();
            return playerPosition.Tween;
        }

        public static MintTween RotateTo(this Transform transform, Vector3 startEuler, Vector3 endEuler, float delay, MintEaseMethod easeMethod = MintEaseMethod.Linear, bool isLocal = false, Action callback = null)
        {
            MintPlayerEuler playerPosition = new MintPlayerEuler();
            playerPosition.StartValue = startEuler;
            playerPosition.EndValue   = endEuler;
            playerPosition.TweenOptions = new MintTweenOptions()
                                          {
                                              Duration = delay,
                                              EaseType = easeMethod
                                          };
            playerPosition.IsLocal          = isLocal;
            playerPosition.IsAutoStartValue = false;
            playerPosition.Init(transform);
            playerPosition.Tween.OnComplete += callback;
            playerPosition.Tween.Start();
            return playerPosition.Tween;
        }

    }
}